package com.rrd.dw.mr.gzip.bhzx;

import java.text.DecimalFormat;
import java.util.Map;
import java.util.regex.Pattern;

import com.rrd.dw.utils.DateUtil;
import com.rrd.dw.utils.MD5Util;

public class BHZXUtils {
	public static final String CRT_FLAG = "\001";
    public static final Pattern CRT_PATTERN = Pattern.compile(CRT_FLAG);
	public static String time_to_date(String datetime){
		 if(datetime==null || datetime.equals("")){
			 return "";
		 }else{
			 return datetime.substring(0, 10);
		 }
	 }
	/**
* map to string
* @param map
* @return
*/
public static String processToString(Map<String,String> map){
	map.put("op_code", "A"); 
	StringBuilder sb = new StringBuilder();
	String req_id=MD5Util.getMd5(map.get("loan_id")+map.get("term_no")+map.get("status_confirm_at")+map.get("term_status"));
//	sb.append(MD5Util.getMd5(map.get("repay_plan_key")+map.get("status_confirm_at"))).append(CRT_FLAG);
	sb.append(map.get("loan_key")).append(CRT_FLAG);
	sb.append(map.get("repay_plan_key")).append(CRT_FLAG);
	sb.append(req_id).append(CRT_FLAG);
	sb.append("A").append(CRT_FLAG); //op_code
	sb.append(DateUtil.getCurrentDateTime()).append(CRT_FLAG);
	sb.append(map.get("loan_id")).append(CRT_FLAG);
	sb.append(map.get("name")).append(CRT_FLAG);
	sb.append(map.get("pid")).append(CRT_FLAG);
	sb.append(map.get("mobile")).append(CRT_FLAG);
	sb.append(map.get("term_no")).append(CRT_FLAG);
	sb.append(map.get("term_status")).append(CRT_FLAG);
	sb.append(map.get("target_repayment_date")).append(CRT_FLAG);
	sb.append(map.get("real_repayment_date")).append(CRT_FLAG);
	sb.append(formateDecimal(map.get("planned_payment"))).append(CRT_FLAG);
	sb.append(formateDecimal(map.get("target_repayment"))).append(CRT_FLAG);
	sb.append(formateDecimal(map.get("real_repayment"))).append(CRT_FLAG);
	sb.append(map.get("overdue_status")).append(CRT_FLAG);
	sb.append(map.get("status_confirm_at")).append(CRT_FLAG);
	sb.append(formateDecimal(map.get("overdue_amount"))).append(CRT_FLAG);
	sb.append(formateDecimal(map.get("remaining_amount"))).append(CRT_FLAG);
	sb.append(map.get("loan_status")).append(CRT_FLAG);
	sb.append(map.get("act_repay_time")).append(CRT_FLAG);
	sb.append(map.get("last_due_date")).append(CRT_FLAG);
	sb.append(map.get("loan_amt")).append(CRT_FLAG);
	sb.append(map.get("loan_payoff_time")).append(CRT_FLAG);
	sb.append(map.get("advance_payoff_time")).append(CRT_FLAG);
	sb.append(map.get("transfer_type")).append(CRT_FLAG);
	sb.append(map.get("transfer_time")).append(CRT_FLAG);
	sb.append(map.get("inv_loan_id")).append(CRT_FLAG);
	sb.append(map.get("loan_platform")).append(CRT_FLAG);
	sb.append(map.get("yx_operatetime")).append(CRT_FLAG);
	sb.append(map.get("yx_overdue_state")).append(CRT_FLAG);
	sb.append(map.get("period_list"));
	return sb.toString();
	 
}
public static String formateDecimal(String amt){
	String r="";
	if(Double.parseDouble(amt)==0){
		return "0";
	}
	if(amt.indexOf(".")>0 ){
		DecimalFormat df = new DecimalFormat("######.00");
		r= df.format(Double.parseDouble(amt));
		return r;
	}else{
		return amt;
	}
}

public static void main(String[] args) {
	DecimalFormat df = new DecimalFormat("######.00");
	String d ="0.0";
	System.out.println(formateDecimal(d));
	StringBuilder sb = new StringBuilder();
	sb.append("a,");
	System.out.println(sb.substring(0, sb.length()-1));
}
   
}
